package com.ilovepostcard;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.ilovepostcard.controller.UserController;
import com.ilovepostcard.controller.server.IServer;
import com.ilovepostcard.controller.server.PostcardResult;
import com.ilovepostcard.controller.server.ServerFactory;
import com.ilovepostcard.model.User;
import com.ilovepostcard.view.MyEditText;

public class FeedbackActivity extends BaseActivity implements OnClickListener{
	
	MyEditText mInput;
	//TextView mCounter;
	Button mConfirm;
	@Override
    public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feedback);
		init();
	}

	private void init()
	{
		mInput = (MyEditText)findViewById(R.id.feedback_input);
		mConfirm = (Button)findViewById(R.id.feedback_submmit);
		mConfirm.setOnClickListener(this);
		
		//mCounter = (TextView)findViewById(R.id.Counter);
		/*mInput.addTextChangedListener(new TextWatcher() {   
            @Override  
            public void onTextChanged(CharSequence s, int start, int before, int count) {   

            	initEditText(s== null ? null : s.toString());
				
            }   
            @Override  
            public void beforeTextChanged(CharSequence s, int start, int count,   
                    int after) {   

            }   
            @Override  
            public void afterTextChanged(Editable s) {   
            	
            
            }   
        }); */ 
	}

	private void initEditText(String s)
	{
		int total = s == null ? 0 : s.length();
    	String message = getResources().getString(R.string.edit_counter);
		String formatMSG = String.format(message, total);
		//mCounter.setText(formatMSG);

		//mCounter.setTextColor(total>=140 ? Color.RED : getResources().getColor(R.color.color_gray_2));
	}

	
	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.feedback_submmit)
		{
			String feedback = null;
			if(mInput.getEditableText()!= null)
			{
				feedback =  mInput.getEditableText().toString();
			}
			
			
			if(feedback!= null&& !feedback.equals(""))
			{
				
				 new FeedbackThread(this).execute(feedback);
				
			}
			
			 finish();
		}
		
	}
	
	class FeedbackThread extends DialogAsyncTask
	{
		
		public FeedbackThread(Activity a) {
			super(a);
			
		}
		
		@Override
		public Object doInBackground(Object... params) {
			
			String feedback = (String)params[0];
			super.doInBackground(params);
			User user = UserController.getLoginUser(getActivity());
			IServer server = ServerFactory.getServer();
			PostcardResult res = server.postProposal(user.UserID, feedback);
			return res;
		}
		@Override
		public void onPostExecute(Object result)
		{
			PostcardResult res = (PostcardResult)result;
			super.onPostExecute(result);
			String resString =  res.isSuccess() ? getActivity().getResources().getString(R.string.feedback_success) : res.getErrorMsg(getActivity());
			Toast.makeText(getActivity(), resString, Toast.LENGTH_SHORT).show();

		}
		
	}
}
